$(document).ready(function () {
  var table = $('table#table').DataTable({
      initComplete: function(settings, json) {
          $("#table").removeClass('dataTable');
          $("#table").parent().addClass('table-responsive');

          $("#table").parent()
              .css('max-height', $(window).height() - $('#query-area').height() - 260)
              .css('height', 'auto')
              .css('overflow-y', 'visible');

          $("#table").on('change', 'input[type="checkbox"]', function () {
              if($(this).prop('checked')) {
                  $(this).closest('tr').addClass('warning');
              } else {
                  $(this).closest('tr').removeClass('warning');
              }
          });

        // 动态添加搜索条件行间距样式
        $('<style>').text(`#query-area .form-group {margin-bottom: 15px;}`).appendTo('head');

        // 动态添加表头居中样式
        $('<style>').text('#table thead th { text-align: center !important; }').appendTo('head');

        // 添加行点击事件处理
        $("#table tbody").on('click', 'tr', function(e) {
          // 避免点击复选框本身时触发两次
          if (!$(e.target).is('input[type="checkbox"]')) {
            // 找到当前行的复选框
            var checkbox = $(this).find('input[type="checkbox"]').first();
            // 切换复选框状态
            checkbox.prop('checked', !checkbox.prop('checked'));
            // 触发change事件以更新行样式
            checkbox.trigger('change');
          }
        });
      },
    ajax: {
      url: baseUrl + '/datatable',
      data : function(d) {
          var material     = $('.search-form').find('input[name="material"]').val();
          var wbsnum       = $('.search-form').find('input[name="wbsnum"]').val();
          var towbsnum     = $('.search-form').find('input[name="towbsnum"]').val();
          var fromdate     = $('.search-form').find('input[name="fromdate"]').val();
          var todate       = $('.search-form').find('input[name="todate"]').val();

          d.material   = material;
          d.wbsnum     = wbsnum;
          d.towbsnum   = towbsnum;
          d.fromdate   = fromdate;
          d.todate     = todate;
	      return d;
		}
    },
    order: [
      [1, 'asc']
    ],
    columns: [{
      data: 'id',
      sortable: false,
      defaultContent: '',
      'class': 'text-center',
      render: function (data, type, row, meta) {
          return '<input type="checkbox" name="ids" value="' + data + '" style="transform: scale(1.5);"/>'
      }

     },{
      'class': 'text-center',
      name: 'supplierCode',
      data: 'supplierCode'
    }, {
      'class': 'text-center',
      name: 'materialCode',
      data: 'materialCode'
    }, {
      name: 'materialName',
      data: 'materialName'
    }, {
      name: 'udf04',
      data: 'udf04',
      render: function (data, type, row, meta) {
        return '';
      }
    }, {
      name: 'wbsNum',
      data: 'wbsNum'
    }, {
      name: 'toWbsNum',
      data: 'toWbsNum'
    },{
      'class': 'text-center',
      name: 'quantity',
      data: 'quantity'
    },{
      'class': 'text-center',
      name: 'lastModifiedDate',
      data: 'lastModifiedDate'
    }]
  });

    $('.form_datetime').datetimepicker({
        format: 'yyyy-mm-dd',
        weekStart: 1,
        autoclose: true,
        startView: 2,
        minView: 2,
        forceParse: false,
        language: 'cn',
        calendarWeeks: true,
        pickerPosition: "bottom-left"
    }).on('changeDate', function (ev) {
        daycount = 0;
        if (null != ev.date && undefined != ev.date) {
            var day = new Date(ev.date.getYear(), ev.date.getMonth(),ev.date.getDate());
            daycount = day.getDate();
        }

    });

  $('.search-form').on("submit", function (e) {
    table.draw();
    //阻止表单submit
    return false;
  });

});
